<template>
  <div class="dashboard-container">
    <!-- <div class="dashboard-text">name: {{ name }}</div> -->
    <el-card class="box-card">
      <div style="display: flex;justify-content: space-between;">
        <div v-for="(o, index) in totalData" :key="index" class="total">
          <div :class="'icon'+index">
            <i v-if="index === 0" style="font-size: 30px" class="el-icon-document-add" />
            <i v-if="index === 1" style="font-size: 30px" class="el-icon-document-checked" />
            <i v-if="index === 2" style="font-size: 30px" class="el-icon-document-checked" />
            <i v-if="index === 3" style="font-size: 30px" class="el-icon-document-checked" />
            <i v-if="index === 4" style="font-size: 30px" class="el-icon-document-checked" />
          </div>
          <div class="right">
            <p class="value">{{ o.value }}</p>
            <p class="label">{{ o.name }}</p>
          </div>
        </div>

      </div>
    </el-card>
    <el-card style="margin: 20px 0;">
      <div slot="header" class="clearfix">
        <span>待处理工单</span>
        <!-- <el-button style="float: right; padding: 3px 0" type="text">操作按钮</el-button> -->
      </div>
      <el-table
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column prop="name" label="任务名称" width="510">
          <template slot-scope="scope">
            <div>{{ scope.row.task && scope.row.task.name }}</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="id"
          label="工单编号"
          width="180"
        />
        <el-table-column prop="status" label="任务状态" width="210">
          <template slot-scope="scope">
            <div>{{ getStatus(scope.row.status) }}</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="city"
          label="提交县区"
          width="210"
        />
        <el-table-column prop="time" label="提交时间">
          <template slot-scope="scope">
            <div>{{ getTimeFormate(scope.row.updated_at) }}</div>
          </template>
        </el-table-column>
        <!-- <el-table-column label="操作" width="210" align="center">
          <template slot-scope="scope">
            <el-button type="primary">处理</el-button>
          </template>
        </el-table-column> -->
      </el-table>
      <Pagination :total="total" :limit="10" :background="false" :page="currentPage" @pagination="handleCurrentChange" @pageSize="handleSizeChange" />
    </el-card>
    <el-card>
      <div slot="header" class="clearfix">
        <span>任务统计</span>
        <!-- <el-button style="float: right; padding: 3px 0" type="text">操作按钮</el-button> -->
      </div>
      <div style="display: flex;flex-wrap: wrap;">
        <div v-for="item in taskList" :key="item.id" style="width: 48%;margin-right: 20px;margin-bottom: 20px">
          <h5 style="margin-bottom: 10px">{{ item.name }}</h5>
          <div :id="echarts(item.id)" style="width: 100%; height: 400px;">
            {{ initEcharts(item) }}
          </div>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
import moment from 'moment'
import { getHomeTotal } from '@/api/table'
import { mapGetters } from 'vuex'
import * as echarts from 'echarts'

export default {
  name: 'Dashboard',
  data() {
    return {
      publishStatus: [
        {
          value: '1',
          label: '未开始'
        },
        {
          value: '2',
          label: '进行中'
        },
        {
          value: '3',
          label: '已完成'
        },
        {
          value: '4',
          label: '已延期'
        },
        {
          value: '5',
          label: '已撤回'
        }
      ],
      totalItem: {},
      total: 0,
      echartsTotal: 0,
      currentPage: 1,
      pageSizes: 10,
      tableData: [],
      taskList: [],
      totalData: [
        {
          name: '本月新增工单',
          value: ''
        },
        {
          name: '本月待办任务',
          value: ''
        },
        {
          name: '本月完结任务',
          value: ''
        },
        {
          name: '本月生成报表',
          value: ''
        },
        {
          name: '本月上报报表',
          value: ''
        }
      ]
    }
  },
  computed: {
    ...mapGetters([
      'name'
    ])
  },
  created() {
    this.getHome()
  },
  mounted() {

  },
  methods: {
    echarts(i) {
      return 'echarts' + i
    },
    initEcharts(item) {
      setTimeout(() => {
        var newData = [
          {
            name: '待处理',
            value: item.todo_count,
            proportion: item.todo_count
          },
          {
            name: '已完结',
            value: item.complete_count,
            proportion: item.complete_count
          },
          {
            name: '已退回',
            value: item.reject_count,
            proportion: item.reject_count
          }
        ]
        var myColor = ['#0099ff', '#51d351', '#ffa94c', '#02A7F0', '#999999']
        var myChart = echarts.init(document.getElementById(`echarts${item.id}`))
        myChart.setOption({
          backgroundColor: '#f2f6fc',
          tooltip: {
            trigger: 'axis',
            show: false,
            axisPointer: { // 坐标轴指示器，坐标轴触发有效
              type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
            }
          },
          legend: {
            show: false
          },
          grid: {
            left: '10%',
            right: '10%',
            bottom: '0%',
            top: '0',
            containLabel: false
          },
          xAxis: [{
            splitLine: {
              show: false
            },
            type: 'value',
            show: false
          }],
          yAxis: [{
            splitLine: {
              show: false
            },
            axisLine: { // y轴
              show: false
            },
            type: 'category',
            axisTick: {
              show: false
            },
            inverse: true,
            data: newData && newData.length > 0 ? newData.map((i) => i.name) : [],
            axisLabel: {
              color: '#676767',
              fontSize: 16
            }
          }],
          series: [{
            name: '',
            type: 'bar',
            barWidth: 12, // 柱子宽度
            label: {
              show: true,
              position: 'right', // 位置
              color: '#FFAA00',
              fontSize: 14,
              distance: 10, // 距离
              formatter: '{c}' // 这里是数据展示的时候显示的数据
            }, // 柱子上方的数值
            itemStyle: {
              barBorderRadius: [0, 0, 0, 0], // 圆角（左上、右上、右下、左下）
              color: function(params) {
                return myColor[params.dataIndex]
              }
            },
            data: newData
          }]
        })
      }, 1000)
    },
    getTimeFormate(date) {
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    },
    getStatus(status) {
      let label = ''
      this.publishStatus.forEach(item => {
        if (item.value == status) {
          label = item.label
        }
      })
      return label
    },
    handleSizeChange(val) {
      this.pageSizes = val.limit
      this.getHome()
    },
    handleCurrentChange(val) {
      this.currentPage = val.page
      this.getHome()
    },
    getHome() {
      getHomeTotal({ page: this.currentPage, per_page: this.pageSizes }).then(res => {
        this.totalItem = res.data
        this.tableData = res.data.sub_task_list.data
        this.totalData[0].value = res.data.sub_task_create_count
        this.totalData[1].value = res.data.task_to_do_count
        this.totalData[2].value = res.data.task_complete_count
        this.totalData[3].value = res.data.aggregate_create_count
        this.totalData[4].value = res.data.aggregate_fix_report_count
        this.taskList = res.data.task_list
        this.total = res.data.sub_task_list.total
        console.log(res, '---------------------------getHomeTotal')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 0 30px;
}
.total {
  display: flex;
  width: 200px;
  align-items: center;
  .right {
    .label {
      color: #999999;
      font-size: 14px;
      margin-top: 5px;
    }
    .value {
      font-size: 24px;
      font-weight: 500;
      color: #666666;
    }
  }
  .icon0 {
    width: 50px;
    height: 50px;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 5px;
    background-color: #fb6260;
    margin-right: 20px;
    color: #fff;
  }
  .icon1 {
    width: 50px;
    height: 50px;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 5px;
    background-color: #73a0fa;
    margin-right: 20px;
    color: #fff;
  }
  .icon2 {
    width: 50px;
    height: 50px;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 5px;
    background-color: #52c1f5;
    margin-right: 20px;
    color: #fff;
  }
  .icon3 {
    width: 50px;
    height: 50px;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 5px;
    background-color: #ff7a8c;
    margin-right: 20px;
    color: #fff;
  }
  .icon4 {
    width: 50px;
    height: 50px;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 5px;
    background-color: #51d351;
    margin-right: 20px;
    color: #fff;
  }
}
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>
